# 进行键盘控制操作
import sys
import os
import pygame

pygame.init()

size = width, height = 600, 400
BLACK = (0, 0, 0)
speed = [1, 1]
fps = 100

screen = pygame.display.set_mode(size)
pygame.display.set_caption('ball3')
BASE_DIR = os.path.realpath(os.path.dirname(sys.argv[0]))
BALL_DIR = os.path.join(BASE_DIR, 'ball.gif')
ball = pygame.image.load(BALL_DIR)
ball_rect = ball.get_rect()

clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                # (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))中
                # (abs(speed[0]) - 1)代表绝对速度减1
                # int(speed[0]/abs(speed[0]))取处原始的方向，这个式子得到的只能是0或1
                speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))

    ball_rect = ball_rect.move(speed[0], speed[1])

    if ball_rect.left < 0 or ball_rect.right > width:
        speed[0] = -speed[0]
    if ball_rect.top < 0 or ball_rect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(BLACK)
    screen.blit(ball, ball_rect)
    clock.tick(fps)
    pygame.display.update()
